import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { AddBankPage } from './addBank';
import { HttpService } from '../../../service/httpService';
import { UserService } from '../../../service/userService';
@Component({
  selector: 'page-bank',
  templateUrl: 'bank.html',
})
export class BankPage {
  private banks:any;
  constructor(public navCtrl: NavController,
              public http:HttpService,
              public user:UserService,
              public navParams: NavParams) {
    this.getUserBankList();
  }

  getUserBankList(){
    let res = JSON.parse(JSON.stringify(this.user.banks));
    for(var i=0;i<res.length;i++){
      let item = res[i];
      item.Bank.LogoImg = this.http.imageUrl +item.Bank.LogoImg;
      if(item.AccountNo && item.AccountNo.length > 8){
        item.AccountNo = item.AccountNo.substring(0,4)+"****"+item.AccountNo.substring(item.AccountNo.length - 4,item.AccountNo.length);
      }
    }
    this.banks = res;
  }

  removeBank(index){

    let bank = this.banks[index];

    let obj = {
      userName : this.user.userName,
      CardId :bank.Id+""
    };
    // let url = this.http.baseUrl + "/api/User/RemoveUserBankDefaultCard";
    let url = this.http.baseUrl + "/api/User/UpdateUserBankDefaultCard";
    this.http.post(url,obj,function (res,err) {
      if(!err){
        console.log(res);
      }
    });
  }


  addBankAction(){
    this.navCtrl.push(AddBankPage);
  }
}
